package ai.chat2db.server.web.start.config.oauth;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Asu
 * @version 1.0
 * @date 2024/5/20 11:07
 * @desc 用户登录、验权工具类
 */
@Component
public class OauthUtil {

    private static final Map<String, OauthService> OAUTH_SERVICE_MAP = new HashMap<>();

    private static String DEFAULT_LOGIN_TYPE;

    @Value("${chat2db.default.loginType}")
    private String defaultLoginType;

    @Resource
    private List<OauthService> oauthServices;

    /**
     * 获取默认登录的service
     *
     * @return defaultOauthService
     */
    public static OauthService getDefaultOauthService() {
        return OAUTH_SERVICE_MAP.get(DEFAULT_LOGIN_TYPE);
    }

    /**
     * 获取三方登录的service
     *
     * @param loginType 三方登录方式
     * @return 三方登录service
     */
    public static OauthService getCustomerOauthService(String loginType) {
        return OAUTH_SERVICE_MAP.get(loginType);
    }

    @PostConstruct
    public void init() {
        for (OauthService os : oauthServices) {
            OAUTH_SERVICE_MAP.put(os.oauthType(), os);
        }
        DEFAULT_LOGIN_TYPE = this.defaultLoginType;
    }

}
